package fr.inserm.umr915.vcfserver.bio;

import java.util.Arrays;
import java.util.List;

public class HumanChromosomes
	implements Chromosomes
	{
	private static final String[] CHROMOSOMES=new String[]{
		"chr1",
		"chr2",
		"chr3",
		"chr4",
		"chr5",
		"chr6",
		"chr7",
		"chr8",
		"chr9",
		"chr10",
		"chr11",
		"chr12",
		"chr13",
		"chr14",
		"chr15",
		"chr16",
		"chr17",
		"chr18",
		"chr19",
		"chr20",
		"chr21",
		"chr22",
		"chrX",
		"chrY"
		};
	
	public HumanChromosomes()
		{
		
		}
	@Override
	public List<String> getNames()
		{
		return Arrays.asList(CHROMOSOMES);
		}
	@Override
	public boolean containsName(String s)
		{
		return getIdByName(s)!=-1;
		}
	@Override
	public boolean containsId(byte b)
		{
		return getNameById(b)!=null;
		}
	
	@Override
	public String getNameById(byte b)
		{
		if(b<1 || b>24) return null;
		return CHROMOSOMES[b-1];
		}
	
	@Override
	public byte getIdByName(String s)
		{
		if(s.startsWith("chr")) s=s.substring(3);
		while(s.startsWith("0")) s=s.substring(1);
		if(s.length()==1)
			{
			char c= s.charAt(0);
			if(c=='X' || c=='x') return (byte)23;
			if(c=='Y' || c=='y') return (byte)24;
			if(c!='0' &&  Character.isDigit(c)) return (byte)(c-'0');
			}
		else if(s.length()==2)
			{
			char c1= s.charAt(0);
			char c2= s.charAt(1);
			if( Character.isDigit(c1) && 
				Character.isDigit(c2))
				{
				int n=((c1-'0')*10+(c2-'0'));
				if(n>0 && n<23) return (byte)n;
				}
			}
		return (byte)(-1);
		}
	@Override
	public GeneticCode getGeneticCodeByName(String s) {
		return GeneticCode.getStandard();
		}
	}
